package com.binance.notification.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.binance.master.models.APIResponse;
import com.binance.notification.api.JApi;
import com.binance.notification.web.service.JService;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class JController implements JApi {

    @Autowired
    private JService jService;

    @Override
    public void getResources(HttpServletResponse response) {
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            response.reset();
            response.setHeader("Content-Disposition", "attachment;filename=localeResources.zip");
            response.setContentType("application/octet-stream; charset=utf-8");
            byte[] buf = this.jService.getI18nResources();
            os.write(buf);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            log.error("下载国际化资源文件出错", e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("", e);
                }
            }
        }
    }

    @Override
    public APIResponse<String> getApiUrl() {
        return APIResponse.getOKJsonResult(this.jService.getApiUrl());
    }

}
